<?php

namespace app\admin\controller;


use app\admin\model\TalkModel;


class Talk extends Base
{
    //话题列表
    public function index()
    {
        if (request()->isAjax()) {
            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['name'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $talk = new TalkModel();
            $selectResult = $talk->getTalksByWhere($where, $offset, $limit);

            $status = config('talk_status');

            // 拼装参数
            foreach($selectResult as $key=>$vo){
                // 不允许操作超级管理员
                if(1 == $vo['id']){
                    $selectResult[$key]['operate'] = '';
                    continue;
                }

                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
                $selectResult[$key]['status'] = showOperate($this->status($vo['id'],$vo['status']));

            }

            $return['total'] = $talk->getAllTalks($where);  //总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    // 添加话题
    public function talkAdd()
    {
        if (request()->isPost()) {

            $param = input('post.');

//            $param['password'] = md5($param['password']);
            $talk = new TalkModel();
            $flag = $talk->insertTalk($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }



        return $this->fetch();
    }

    // 编辑话题
    public function talkEdit()
    {
        $talk = new TalkModel();

        if(request()->isPost()){

            $param = input('post.');
            $flag = $talk->editTalk($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');
        $this->assign([
            'talk' => $talk->getOneTalk($id)
        ]);
        return $this->fetch();
    }

    // 删除话题
    public function TalkDel()
    {
        $id = input('param.id');

        $talk = new TalkModel();
        $flag = $talk->delTalk($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'role/roleedit',
                'href' => url('talk/talkEdit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'role/roledel',
                'href' => "javascript:talkDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }

    //改变状态
    public function statusEdit()
    {
        $id = input('param.id');
        $s =input('param.status');
        $talk = new TalkModel();
        $flag = $talk->editStatus($id,$s);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
//状态按钮 1 为进行中 2为结束
    public function status($id,$s)
    {
        if ($s==1){
            return [
                '启用' => [
                    'auth' => 'role/roleedit',
                    'href' => "javascript:statusEdit(" . $id .",". $s.")",
                    'btnStyle' => 'info',
                    'icon' => 'fa fa-institution'
                ]
            ];
        }else{
            return [
                '禁用' => [
                    'auth' => 'role/roleedit',
                    'href' => "javascript:statusEdit(" . $id."," .$s. ")",//该按钮已失效
                    'btnStyle' => 'info',
                    'icon' => 'fa fa-institution'
                ]
            ];
        }
    }
}
